package thread;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author: feiwang_6
 * @create: 2019/12/5 19:27
 * @description:
 */
public class User {
    public volatile int age;
    public String name;
    
    public User(Integer age, String name) {
        this.age = age;
        this.name = name;
    }
    
    public void safeOperator(AtomicReference<User> atomic){
        while (true){
            User expect = atomic.get();
            User update = new User(expect.getAge() + 1, "name"+(expect.getAge() + 1));
            boolean b = atomic.compareAndSet(expect, update);
            if(b){
                return;
            }
        }
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
